//
//  BaseViewController.m
//  wtshoppingcite
//
//  Created by xiangming on 14-10-15.
//  Copyright (c) 2014年 TW. All rights reserved.
//

#import "BaseViewController.h"
#import "MainViewController.h"
#import "Macro.h"

@interface BaseViewController ()<MBProgressHUDDelegate>

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UITextField appearance] setTintColor:[UIColor grayColor]];
    
    UIImage *image;
    if (Version >= 7.0)
    {
        self.automaticallyAdjustsScrollViewInsets = NO;
        
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        
        image = [UIImage createImageWithBgColor:MAIN_BLUE_COLOR size:CGSizeMake(SCREEN_WIDTH, 40) cornerRadius:0];
        
        if(self.navigationController.viewControllers.count == 1){
            self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        }
        else{
            self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        }
    }
    else{
        
        image = [UIImage createImageWithBgColor:MAIN_BLUE_COLOR size:CGSizeMake(SCREEN_WIDTH, 44) cornerRadius:0];
    }
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    NSShadow *shadow = [[NSShadow alloc] init];
    [shadow setShadowColor: RGBA(233, 79, 18, 1)];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: RGBA(233, 79, 18, 1), NSShadowAttributeName: shadow};
    [self setIsBackButton:YES];
    self.view.backgroundColor = [UIColor whiteColor];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)setIsBackButton:(BOOL)isBackButton
{
    _isBackButton = isBackButton;
    if(_isBackButton){
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        backButton.frame = CGRectMake(0, 10, 20,28);
        [backButton setImage:[UIImage imageNamed:@"image_back_icon"] forState:UIControlStateNormal];
        backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 5, 0);
        [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
        
        [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        leftItem.title = @"返回";
        self.navigationItem.leftBarButtonItem = leftItem;
//        [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
//        self.navigationItem.backBarButtonItem.title = @"";
//        [[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];
//        
    }
}


//模态视图返回
- (void)setIsRightMenuButton:(BOOL)isRightMenuButton
{
    _isRightMenuButton = isRightMenuButton;
    if(_isRightMenuButton){
        _menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _menuButton.frame = CGRectMake(0, 40.0/2-32.0/2.0, 40, 32);
        [_menuButton addTarget:self action:@selector(menuAction) forControlEvents:UIControlEventTouchUpInside];
        //        [_menuButton setImage:[UIImage imageNamed:@"lefts.png"] forState:UIControlStateNormal];
        [_menuButton setTitle:@"右边" forState:UIControlStateNormal];
        [_menuButton setTitleColor:MAIN_BLACK_COLOR forState:UIControlStateNormal];
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:_menuButton];
        self.navigationItem.rightBarButtonItem = rightItem;
        
    }
}

//模态视图返回
- (void)setIsCancelButton:(BOOL)isCancelButton
{
    _isCancelButton = isCancelButton;
    if(_isCancelButton){
        _cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancleButton.frame = CGRectMake(0, 44.0/2-30.0/2.0, 23, 23);
        [_cancleButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        [_cancleButton setImage:[UIImage imageNamed:@"button_chazi.png"] forState:UIControlStateNormal];
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:_cancleButton];
        self.navigationItem.leftBarButtonItem = leftItem;
        
    }
}

- (void)setTitleView:(NSString *)titleName
{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    titleView.backgroundColor = [UIColor clearColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 7,200, 30)];
    titleLabel.text = titleName;
    titleLabel.font = [UIFont boldSystemFontOfSize:20.0f];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:titleLabel];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.backgroundColor = [UIColor clearColor];
    self.navigationItem.titleView = titleView;
    
}

-(void)setRightButton:(NSString*)titleName sel:(SEL)selector viewController:(UIViewController*)viewController{
    CGSize size = [titleName getSizeOfStringFontSize:16 constroSize:CGSizeMake(120, MAXFLOAT)];
    UIButton* button = [UnitToolCommonView viewWithButtonWithText:titleName textColor:[UIColor whiteColor] frame:CGRectMake(0, 0, size.width, 40)];
    button.userInteractionEnabled = YES;
    [button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)cancelAction
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}



-(UILabel*)createLabelWithFrame:(CGRect)frame text:(NSString*)text font:(int)font textAli:(NSTextAlignment)alig textColor:(UIColor*)tc{
    if(text == nil)
        text = @"";
    UILabel* label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = alig;
    label.textColor = tc;
    label.backgroundColor = [UIColor clearColor];
    return label;
}


//创建UITextField
- (UITextField *)createTextFieldWithFrame:(CGRect)frame text:(NSString *)txt fontSize:(int)size
{
    UITextField * textField = [[UITextField alloc] initWithFrame:frame];
    //    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.textColor = [UIColor darkGrayColor];
    textField.font = [UIFont systemFontOfSize:size];
    textField.textAlignment = NSTextAlignmentLeft;
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    textField.returnKeyType = UIReturnKeyNext;
    textField.text = txt;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    return textField;
}

#pragma mark - alert Methods
- (void)showAlertView:(NSString *)title message:(NSString *)msg
{
    CHECK_STRING_IS_NULL(title);
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:title
                                                         message:msg
                                                        delegate:nil
                                               cancelButtonTitle:@"确定"
                                               otherButtonTitles:nil];
    [alertView show];
}

- (void)showToastWithMessage:(NSString *)message showTime:(float)interval
{
    TipLabel * tip = [[TipLabel alloc] init];
    [tip showToastWithMessage:message showTime:interval];
}


#pragma mark - HUD　Methods

#pragma mark - MBProgressHUD Method
- (void)showProgressHUD
{
    if (!self.hud) {
        self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    }
}
- (void)hideProgressHUD
{
    if (self.hud) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }
    self.hud = nil;
}

- (void)showProgressHUDWithTitle:(NSString *)title{
    
    if (!self.hud) {
        self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    }
    self.hud.labelText = title;
}


- (void)showToastMessage:(NSString *)message
{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithFrame:self.view.bounds];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = message;
    hud.labelFont = [UIFont boldSystemFontOfSize:14];
    [self.view addSubview:hud];
    [hud show:YES];
    [hud hide:YES afterDelay:1.5];
}


- (void)showLoadImageView:(UIView*)supview subview:(UIView*)view{
    
    if(_loadImageView == nil){
        _loadImageView = [[UIImageView alloc] initWithFrame:view.frame];
        _loadImageView.contentMode = UIViewContentModeScaleAspectFit;
        _loadImageView.backgroundColor = [UIColor whiteColor];
        if(IS_4_INCH_SCREEN){
            _loadImageView.animationImages = @[[UIImage imageNamed:@"waiting1_6.png"],[UIImage imageNamed:@"waiting2_6.png"]];
        }else{
            _loadImageView.animationImages = @[[UIImage imageNamed:@"waiting-4-01.png"],[UIImage imageNamed:@"waiting-4-02.png"]];
        }
        _loadImageView.animationDuration = 0.15;
        _loadImageView.animationRepeatCount = 1000;
        [_loadImageView startAnimating];
        [supview addSubview:_loadImageView];
    }
}
- (void)dismissLoadImageView{

    [_loadImageView removeFromSuperview];
    _loadImageView = nil;
    
}

#pragma  mark LoginAciton
-(void)LoginAciton{
    NSLog(@"进入登录");
    if (![APP_DELEGATE isLogin]) {
        [APP_DELEGATE pushLoginViewControllerWithStoryBoardIdentifier:@"LoginViewController" pushIn:self];
    }
    
}



-(UIView*)createFavViewWithFrame:(CGRect)rect{
    
    UIView* view = [[UIView alloc] initWithFrame:rect];
    view.backgroundColor = [UIColor whiteColor];
    UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, (rect.size.height - 40)/2, 40, 40)];
    imageView.backgroundColor = COLOR_RGB(216, 201, 206);
    [imageView.layer setCornerRadius:imageView.width/2];
    imageView.clipsToBounds = YES;
    imageView.tag = 1000;
    [view addSubview:imageView];
    
    UILabel* titleLabel = [self createLabelWithFrame:CGRectMake(imageView.right+5, 10, 120, 20) text:@"11111111" font:18.0 textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
    titleLabel.tag = 1001;
    [view addSubview:titleLabel];
    
    UILabel* subLabel = [self createLabelWithFrame:CGRectMake(imageView.right+5, titleLabel.bottom+3, 120, 20) text:@"222222" font:15.0 textAli:NSTextAlignmentLeft textColor:COLOR_RGB(168, 168, 168)];
    subLabel.tag = 1002;
    [view addSubview:subLabel];
    return view;
}

//加小红点
-(void)addRedFlotMessagePushDataWithUimageView:(UIView*)supView{
    
}

-(void)removeRedFlotMessagePushDataWithUimageView:(UIView*)supView{
    
}


@end
